﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace N03_11HCA_HTTTHD.Controllers
{
    public class ChaoBanController : Controller
    {
        Data_DAO_DTO_Lib.THONGTINCANBAN_DAO cbDAO = new Data_DAO_DTO_Lib.THONGTINCANBAN_DAO();

        Data_DAO_DTO_Lib.TIEUCHUANTHIETBI_DAO tcDAO = new Data_DAO_DTO_Lib.TIEUCHUANTHIETBI_DAO();

        Data_DAO_DTO_Lib.THONGTINTHIETBICONGNGHE_DAO ttDAO = new Data_DAO_DTO_Lib.THONGTINTHIETBICONGNGHE_DAO();

        Data_DAO_DTO_Lib.MUCDOPHATTRIENTHIETBI_DAO mdDAO = new Data_DAO_DTO_Lib.MUCDOPHATTRIENTHIETBI_DAO();

        Data_DAO_DTO_Lib.XUATXUDETAI_DAO ngDAO = new Data_DAO_DTO_Lib.XUATXUDETAI_DAO();

        Data_DAO_DTO_Lib.CHUYENGIAO_DAO cgDAO = new Data_DAO_DTO_Lib.CHUYENGIAO_DAO();

        Data_DAO_DTO_Lib.CUNGCAP_DAO ccDAO = new Data_DAO_DTO_Lib.CUNGCAP_DAO();

        Data_DAO_DTO_Lib.DANHMUC_DAO dmDAO = new Data_DAO_DTO_Lib.DANHMUC_DAO();
        // GET: /ChaoBan/

        Data_DAO_DTO_Lib.DANHSACHCANBANQUANTAM_DAO cbQTDAO = new Data_DAO_DTO_Lib.DANHSACHCANBANQUANTAM_DAO();
        [HttpPost]
        public JsonResult QuanTam(int id)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);
            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng quan tâm cần bán" });
            }

            try
            {
                Data_DAO_DTO_Lib.DTO.DANHSACHCANBANQUANTAM_DTO rvQT = new Data_DAO_DTO_Lib.DTO.DANHSACHCANBANQUANTAM_DTO();
                rvQT.IDTaiKhoanQuanTam = (int)N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1;
                rvQT.IDThongTinCanBan = id;
                cbQTDAO.Them(rvQT);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }

            return Json(new { Result = "OK" });
        }

        public ActionResult Index()
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> list = cbDAO.LayDanhSachCoThamSo(0, TienIch.HeThong.SoLuongLoadCanBanMacDinh, TienIch.HeThong.ChuoiSapXepCanBanMacDinh);

            Models.ChaoBan model = new Models.ChaoBan(list);

            return View("ChaoBan", model);
        }

        [HttpPost]
        public JsonResult Index(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            List<Data_DAO_DTO_Lib.DTO.DTO> list = cbDAO.LayDanhSachCoThamSo(jtStartIndex, jtPageSize, jtSorting);

            return new JsonResult() { Data = list };
        }


        public ActionResult ChiTiet(int id_TimMua)
        {

            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Data_DAO_DTO_Lib.DTO.THONGTINCANBAN_DTO cbDTO = (Data_DAO_DTO_Lib.DTO.THONGTINCANBAN_DTO)cbDAO.LayTuID(id_TimMua);
            Data_DAO_DTO_Lib.DTO.THONGTINTHIETBICONGNGHE_DTO ttDTO = null;
            if (cbDTO.IDThietBi != null)
            {
                ttDTO = (Data_DAO_DTO_Lib.DTO.THONGTINTHIETBICONGNGHE_DTO)ttDAO.LayTuID((int)cbDTO.IDThietBi);
            }

            List<Data_DAO_DTO_Lib.DTO.DTO> lTieuChuan = tcDAO.LayDanhSach();

            List<Data_DAO_DTO_Lib.DTO.DTO> lMucDo = mdDAO.LayDanhSachCuaChaoBan(cbDTO.ID);

            List<Data_DAO_DTO_Lib.DTO.DTO> lXuatXu = ngDAO.LayDanhSachCuaChaoBan(cbDTO.ID);

            List<Data_DAO_DTO_Lib.DTO.DTO> lChuyenGiao = cgDAO.LayDanhSachCuaChaoBan(cbDTO.ID);

            List<Data_DAO_DTO_Lib.DTO.DTO> lCungCap = ccDAO.LayDanhSachCuaChaoBan(cbDTO.ID);

            List<Data_DAO_DTO_Lib.DTO.DTO> lPhanLoai = dmDAO.LayDanhSachCuaChaoBan(cbDTO.ID);

            Models.ChaoBan_ChiTiet model = new Models.ChaoBan_ChiTiet(cbDTO, lTieuChuan, ttDTO, lMucDo, lXuatXu, lChuyenGiao, lCungCap, lPhanLoai);

            return View("ChaoBan_ChiTiet", model);
        }
    }
}
